package com.xcx.robot.mirai.factory.facade;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.http.HtmlUtil;
import cn.hutool.http.HttpUtil;
import com.xcx.robot.mirai.config.UserConfig;
import com.xcx.robot.mirai.factory.BaseFacade;
import com.xcx.robot.mirai.key.KeyWord;
import com.xcx.robot.mirai.util.KeyUtils;
import com.xcx.robot.mirai.util.SenderHelper;
import com.xcx.robot.util.ImageUtils;
import lombok.extern.slf4j.Slf4j;
import net.mamoe.mirai.contact.Contact;
import net.mamoe.mirai.message.data.ForwardMessage;
import net.mamoe.mirai.message.data.ForwardMessageBuilder;
import net.mamoe.mirai.message.data.PlainText;
import net.mamoe.mirai.message.data.RawForwardMessage;
import net.mamoe.mirai.utils.ExternalResource;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @Author: 邢晨旭
 * @Date: 2021/5/26 16:18
 * @Description:
 */
@Slf4j
@Service
public class MagneticFacade implements BaseFacade {
    public KeyWord keyWord = KeyWord.MAGNETIC;
    @Resource
    UserConfig userConfig;

    @Override
    public void execute(Contact sender, Contact receiver, String content) {
        if((CollectionUtil.isEmpty(userConfig.getImageGroups()))){
            return;
        }
        if(userConfig.getImageGroups().contains(receiver.getId())||userConfig.getGmGroups().contains(receiver.getId())){
            String s = HttpUtil.get("https://3mag.net/search?q="+ KeyUtils.getKeyByWord(content, 2));
            List<String> all = ReUtil.findAll("<a href=\"(.*?)\">", s, 1);
            log.info("all a---{}",all);
            List<String> result = new ArrayList<>();
            int i = 0;
            for (String url : all) {
                if(url.equals("/") || !url.startsWith("/!")){
                    continue;
                }
                String s1 = HttpUtil.get("https://3mag.net"+url);
                List<String> all1 = ReUtil.findAll("<input id=\"input-magnet\" class=\"form-control\" type=\"text\" value=\"(.*?)\" spellcheck=\"false\" />", s1, 1);
                List<String> title = ReUtil.findAll("<meta content=\"(.*?) 种子磁链\" name=\"description\" />", s1, 1);
                String s2 = all1.get(0);
                s2 = title.get(0)+"\n"+s2.substring(0,s2.indexOf("&amp"));
                //magnet:?xt=urn:btih:3756e43d4c6b3a907b3db48255113a365c2986f0&amp
                result.add(s2);
                log.info("磁力 a---{}",s2);
                i++;
                if(i>=5)break;
            }
            ForwardMessageBuilder builder = new ForwardMessageBuilder(receiver);
            result.forEach(msg->{
                builder.add(123456L, "磁力先锋", new PlainText(msg));
            });
            ForwardMessage forward = builder.build();
            receiver.sendMessage(forward);
        }


    }

    @Override
    public KeyWord keyWord() {
        return keyWord;
    }
}
